#include<iostream>
#include<cmath>
#include<cstring>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e5 + 10, INF = 0x3f3f3f3f;

int bit[11] = { 0,1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 };

int n, q;
int block, t;
int st[400], ed[400], pos[N];
int a[N], cnt[400][11][10];

void init()
{
    block = sqrt(n);
    t = (n + block - 1) / block;
    for (int i = 1;i <= t;i++) {
        st[i] = (i - 1) * block + 1;
        ed[i] = i * block;
    }
    ed[t] = n;
    for (int i = 1;i <= n;i++) pos[i] = (i - 1) / block + 1;
    memset(cnt, 0, sizeof cnt);
}

void change(int x, int y, int d)
{
    int p = pos[x];
    for (int i = 1;i <= 10;i++, y /= 10) {
        int t = y % 10;
        cnt[p][i][t] += d;
    }
}

int ask(int l, int r, int d, int x)
{
    int p = pos[l], q = pos[r];
    int ans = 0;
    if (p == q) {
        for (int i = l;i <= r;i++) ans += ((a[i] / bit[d]) % 10 == x);
    } else {
        for (int i = p + 1;i <= q - 1;i++)  ans += cnt[i][d][x];
        for (int i = l;i <= ed[p];i++) ans += ((a[i] / bit[d]) % 10 == x);
        for (int i = st[q];i <= r;i++) ans += ((a[i] / bit[d]) % 10 == x);
    }
    return ans;
}

void solve()
{
    cin >> n >> q;
    init();
    for (int i = 1;i <= n;i++) {
        cin >> a[i];
        change(i, a[i], 1);
    }

    char op; int x, y, d, p;
    while (q--) {
        cin >> op;
        if (op == 'S') {
            cin >> x >> y;
            change(x, a[x], -1);
            change(x, a[x] = y, 1);
        } else {
            cin >> x >> y >> d >> p;
            cout << ask(x, y, d, p) << '\n';
        }
    }
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}